package mxw.suiye.autopydroid.android;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import mxw.suiye.autopydroid.R;
import mxw.suiye.autopydroid.core.app.App;
import mxw.suiye.autopydroid.core.ui.Ui;
import mxw.suiye.autopydroid.util.MyHandler;


public class MainActivity extends AppCompatActivity {

    private TextView serviceText;
    private Switch accessibilitySwitch;
    private Button exitBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bindView();
        eventListen();
    }

    // 双击退出
    private long exitTime;

    @Override
    public void onBackPressed() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            Ui.toast("再按一次进入后台");
            exitTime = System.currentTimeMillis();
        } else {
            finish();
//            HandlerTool.getAppHandle().sendEmptyMessage(0);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        accessibilitySwitch.setChecked(MyHandler.accessibilityStatus());
        serviceText.setText("服务已启动");
    }

    /********自定义方法区*********/


    void bindView() {
        exitBtn = findViewById(R.id.exit);
        serviceText = findViewById(R.id.serverText);
        accessibilitySwitch = findViewById(R.id.accessiblity);
    }

    void eventListen() {
        exitBtn.setOnClickListener(view -> exitApp());

        accessibilitySwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                App.startAcSetting();
            } else {
                MyHandler.closeAccessibility();
            }
        });
    }


    /************功能区************/

    void exitApp(){
        MyHandler.closeAccessibility(); // 退出辅助服务
        MyHandler.closeAppHandle();// 退出app
    }

}
